package cn.yunhe.service.impl;

import cn.yunhe.dao.FieldDao;
import cn.yunhe.entity.Field;
import cn.yunhe.service.FieldService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service("fieldService")
public class FieldServiceImpl implements FieldService {

    @Resource
    private FieldDao fieldDao;


    @Override
    public Page getFieldListByPage(Map<String, Object> map) {
        Page page = new Page();

        //获取总条数
        page.setTotal(fieldDao.getFieldListCountByPage(map));
        //     System.out.println(tagDao.getTagListCountByPage(map));
        // 获取每页大小
        int pageSize = Integer.parseInt(String.valueOf(map.get("pageSize")));
        page.setPageSize(pageSize);

        // 获取当前页码
        int curPageNum = Integer.parseInt(String.valueOf(map.get("pageNum")));
//		page.setPageNum(curPageNum);
        // 计算出总页数
        page.setPageNum((int) (page.getTotal() / pageSize + (page.getTotal() % pageSize == 0 ? 0 : 1)));
        // 启用分页
        page = PageHelper.startPage(curPageNum, pageSize);
        // 查询列表
        List list = fieldDao.getFieldListByPage(map);
        return page;
    }

    @Override
    public int addField(Field field) {

        if (field.getFieldName() == null) {
            return 0;
        } else {
            System.out.println("field     " + field.getFieldName());
            return fieldDao.addField(field);
        }
    }

    @Override
    public int delField(Field field) {
        System.out.println(field.getFieldName());
        return fieldDao.delField(field);
    }

    @Override
    public int updField(Field field) {
        return fieldDao.updField(field);
    }

    @Override
    public Field getFieldById(Field field) {

        return fieldDao.getFieldById(field);
    }

    @Override
    public List<Field> getListField() {
        return fieldDao.getListField();
    }


    /**
     * 获取所有分类
     * @return
     * 郭瀚钰
     */
    @Override
    public List<Field> getAllField() {
       return  fieldDao.getAllField();
    }

    public FieldDao getFieldDao() {
        return fieldDao;
    }

    public void setFieldDao(FieldDao fieldDao) {
        this.fieldDao = fieldDao;
    }
}
